<template>
	<view>
		<header-top :messageInfo="{typeFlag:true,message:'修改密码',liner:true}"></header-top>
		<view class="mainpadding_top">
			<view class="logintext">修改密码</view>
			<view class="mainpadding_top">
				<view class="needbot-bor">
					<input type="number" placeholder="请输入手机号" v-model="mobile" disabled="">
				</view>
				<view class="needbot-bor flexbetween">
					<input type="text" placeholder="请输入手机验证码" v-model="captcha">
					<view class="flexcenter sendcode" @click="sendyzm" v-show="codetext == 1">发送验证码</view>
					<view class="flexcenter sendcode" v-show="codetext == 2">{{second}}s后重新获取</view>
					<view class="flexcenter sendcode" @click="sendyzm" v-show="codetext == 3">重新发送</view>
				</view>
				<view class="needbot-bor">
					<input type="password" placeholder="请输入新密码" v-model="newpassword">
				</view>
				<!-- <view class="flexright nostrongtext margin_top" @click="jumpurl('/pages/login/thelogin')">短信验证码登录</view> -->
			</view>
		</view>
		<view class="mainpadding_top">
			<view class="surebtns flexcenter margin_top" @click="jumpnext">提交</view>
		</view>
		
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				codetext:1,
				second:59,
				mobile:'',
				newpassword:"",
				captcha:'',
			}
		},
		onLoad() {
			this.getInfo()
		},
		methods: {
			getInfo() {
				httpRequest.request('/dhtapi/user/index', 'POST', {}).then(res => {
					this.mobile = res.data.mobile
				}).catch(err => {
					httpRequest.toast(err.msg)
				})
			},
			//验证手机号
			isMobile(value) {
				let a = /^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(value); 
				return a;
			},
			//发送验证码
			sendyzm(){
				let _this = this;
				if(_this.mobile == ''){
					httpRequest.toast("请输入手机号码!")
					return;
				}
				if(!_this.isMobile(_this.mobile)){
					httpRequest.toast("手机号码格式错误!")
					return;
				}
				_this.codetext = 2;
				_this.second = 59;
				let timer = setInterval(function(){
					if(_this.second > 1){
						_this.second--;
					}else{
						clearInterval(timer);
						_this.codetext = 3;
					}
				},1000)
				httpRequest.request('/api/sms/send','POST',{
					mobile:this.mobile,
					event:'dhtchangepwd',
				},false,true,false).then(res => {
					if(res.code == 1){
						httpRequest.toast('发送成功')
					}
				})
			},
			jumpnext(){
				if(this.mobile==""){
					httpRequest.toast("请输入手机号")
					return false
				}
				if(!this.isMobile(this.mobile)){
					httpRequest.toast("手机号码格式错误")
					return false
				}
				if(this.captcha==""){
					httpRequest.toast("请输入验证码")
					return false
				}
				if(this.newpassword==""){
					httpRequest.toast("请输入验证码")
					return false
				}
				httpRequest.request("/dhtapi/user/changepwd", 'POST', {
					mobile:this.mobile,
					newpassword:this.newpassword,
					captcha:this.captcha,
				}, false, false, true).then(res => {
					if(res.code == 1){
						httpRequest.toast("修改成功请重新登录");
						setTimeout(() => {
							// 将存储的数据改为空达到退出登录的效果
							uni.removeStorage('token');
							uni.removeStorage('user_id');
							uni.reLaunch({
								url: "/pages/login/accountlogin"
							})
						}, 1000)
					}else{
						httpRequest.toast(res.msg);
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.logintext{
		font-size: 40rpx;
		font-family: PingFang SC-Bold, PingFang SC;
		font-weight: bold;
		color: #000000;
		line-height: 47rpx;
	}
	.needbot-bor{
		margin-top: 40rpx;
		padding-bottom: 20rpx;
		border-bottom: 1rpx solid rgba(153, 153, 153, 0.06);
	}
	.sendcode{
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #00B855;
	}
	.nostrongtext{
		font-weight: 500;
	}
	.smalltext{
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #999999;
	}
	.maincolor_text{
		color: #0BBDCB;
	}
</style>
